package cn.iocoder.yudao.module.member.api.user;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.util.CheckUtils;
import cn.iocoder.yudao.framework.common.util.CollectionUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.member.api.user.dto.CrmMemberUserRespDTO;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserQueryRespDTO;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.member.api.user.dto.UserRelationDTO;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO;
import cn.iocoder.yudao.module.member.dal.dataobject.tag.MemberTagDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserGasAddressDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.dataobject.userrelation.UserRelationDO;
import cn.iocoder.yudao.module.member.dal.mysql.user.CrmMemberUserMapper;
import cn.iocoder.yudao.module.member.dal.mysql.userrelation.UserRelationMapper;
import cn.iocoder.yudao.module.member.service.level.MemberLevelService;
import cn.iocoder.yudao.module.member.service.tag.MemberTagService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import cn.iocoder.yudao.module.trade.api.delivery.DeliveryPickUpStoreApi;
import cn.iocoder.yudao.module.trade.api.delivery.dto.DeliveryPickUpStoreDOVo;
import cn.iocoder.yudao.module.trade.controller.app.delivery.vo.pickup.AppDeliveryPickUpStoreRespVO;
import cn.iocoder.yudao.module.trade.convert.delivery.DeliveryPickUpStoreConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryPickUpStoreDO;
import cn.iocoder.yudao.module.trade.service.delivery.DeliveryPickUpStoreService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 会员用户的 API 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Lazy(value = true)
public class MemberUserApiImpl implements MemberUserApi {

    @Resource
    private MemberUserService userService;
    @Resource
    private CrmMemberUserMapper crmMemberUserMapper;
    @Resource
    @Lazy
    private MemberLevelService memberLevelService;
    @Resource
    private MemberTagService memberTagService;
    @Resource
    private DeliveryPickUpStoreApi pickUpStoreApi;
    @Resource
    private UserRelationMapper userRelationMapper;

    @Resource
    private DeliveryPickUpStoreService deliveryPickUpStoreService;
    /**
     *  获取燃气户详情
     * @param id 编号
     * @return
     */
    @Override
    public List<CrmMemberUserRespDTO> getCrmMemberByMobile(MemberUserQueryRespDTO memberUserQueryRespDTO) {
        LambdaQueryWrapperX<MemberUserGasAddressDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(MemberUserGasAddressDO::getMobile,memberUserQueryRespDTO.getMobile());
        queryWrapper.eqIfPresent(MemberUserGasAddressDO::getCode,memberUserQueryRespDTO.getUserCode());
        queryWrapper.eqIfPresent(MemberUserGasAddressDO::getCrmName,memberUserQueryRespDTO.getUserName());
        queryWrapper.likeIfPresent(MemberUserGasAddressDO::getDetailAddress,memberUserQueryRespDTO.getAddress());
        queryWrapper.eq(MemberUserGasAddressDO::getStatus,0);
        List<MemberUserGasAddressDO> list = crmMemberUserMapper.selectList(queryWrapper);
        if(!CheckUtils.isEmpty(memberUserQueryRespDTO.getMobile())){
            MemberUserRespDTO memberUserRespDTO = getUserByMobile(memberUserQueryRespDTO.getMobile());
            if(!CheckUtils.isEmpty(memberUserRespDTO)){
                List<UserRelationDO> relationDOList = getUserRelationList(memberUserRespDTO.getId());
                if(!CollectionUtil.isEmpty(relationDOList)){
                    if(CollectionUtil.isEmpty(list)){
                        list = new ArrayList<>();
                    }
                    for(UserRelationDO relationDO : relationDOList){
                        MemberUserGasAddressDO memberUserGasAddressDO = crmMemberUserMapper.selectById(relationDO.getCrmMemberUserId());
                        list.add(memberUserGasAddressDO);
                    }
                }
            }
        }
        if(CollectionUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        for(MemberUserGasAddressDO x : list){
            if (CheckUtils.isEmpty(x.getNearbyStoreId())){
                if(CheckUtils.isEmpty(x.getLnglat())) continue;
                List<DeliveryPickUpStoreDO> storeListByStatus = deliveryPickUpStoreService.getDeliveryPickUpStoreListByStatus(
                        CommonStatusEnum.ENABLE.getStatus());
                if(x.getLnglat().split(",").length == 2){
                    Double xX = Double.valueOf(x.getLnglat().split(",")[0]);
                    Double yY = Double.valueOf(x.getLnglat().split(",")[1]);
                    AppDeliveryPickUpStoreRespVO appDeliveryPickUpStoreRespVO = DeliveryPickUpStoreConvert.INSTANCE.convertMinDistance(storeListByStatus, Double.valueOf(x.getLnglat().split(",")[0]), Double.valueOf(x.getLnglat().split(",")[1]));
                    x.setNearbyStoreId(appDeliveryPickUpStoreRespVO.getId());
                    x.setStoreName(appDeliveryPickUpStoreRespVO.getName());
                    continue;
                }
            }
            DeliveryPickUpStoreDOVo deliveryPickUpStoreDOVo = pickUpStoreApi.selectById(x.getNearbyStoreId());
            if(!CheckUtils.isEmpty(deliveryPickUpStoreDOVo)){
                x.setStoreName(deliveryPickUpStoreDOVo.getName());
            }
        };
        return BeanUtils.toBean(list ,CrmMemberUserRespDTO.class);
    }

    private List<UserRelationDO> getUserRelationList(Long memberUserId) {
        LambdaQueryWrapperX<UserRelationDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(UserRelationDO::getMemberUserId,memberUserId);
        queryWrapper.eq(UserRelationDO::getStatus,0);
        return userRelationMapper.selectList(queryWrapper);
    }


    @Override
    public MemberUserRespDTO getUser(Long id) {
        MemberUserDO user = userService.getUser(id);
        if(CheckUtils.isEmpty(user)){
            return null;
        }
        // 处理用户级别返显
        MemberLevelDO level = memberLevelService.getLevel(user.getLevelId());
        MemberUserRespDTO memberUserRespDTO = MemberUserConvert.INSTANCE.convert2(user);
        if(!CheckUtils.isEmpty(level)){
            memberUserRespDTO.setLevel(level.getName());
        }
        List<MemberTagDO> list = memberTagService.getTagList(user.getTagIds());
        if(!CollectionUtil.isEmpty(list)){
            List<String> tagList = list.stream().map(MemberTagDO::getName).collect(Collectors.toList());
            memberUserRespDTO.setTags(tagList);
        }
        return memberUserRespDTO;
    }

    @Override
    public List<MemberUserRespDTO> getUserList(Collection<Long> ids) {
        return MemberUserConvert.INSTANCE.convertList2(userService.getUserList(ids));
    }

    @Override
    public List<MemberUserRespDTO> getUserListByNickname(String nickname) {
        return MemberUserConvert.INSTANCE.convertList2(userService.getUserListByNickname(nickname));
    }

    @Override
    public MemberUserRespDTO getUserByMobile(String mobile) {
        return MemberUserConvert.INSTANCE.convert2(userService.getUserByMobile(mobile));
    }

}
